//
//  BSRecommendViewController.m
//  sister
//
//  Created by 周冰烽 on 2017/2/14.
//  Copyright © 2017年 周冰烽. All rights reserved.
//

#import "BSRecommendViewController.h"

#import "BSRecommendCategoryViewModel.h"
#import "BSCategoryUserListViewModel.h"
#import "BSRecommendViewModel.h"

#import "BSRecommendCategoryCell.h"
#import "BSUserTableViewCell.h"

#import <UIImageView+WebCache.h>

@interface BSRecommendViewController ()<UITableViewDelegate,UITableViewDataSource>
/** vm */
@property(nonatomic,strong)BSRecommendCategoryViewModel *reVM;
@property(nonatomic,strong)BSCategoryUserListViewModel *userVM;
@property(nonatomic,strong)BSRecommendViewModel *recomVM;
@property(nonatomic,strong)UITableView *leftTableView;
@property(nonatomic,strong)UITableView *rightTableView;
@end

@implementation BSRecommendViewController
static NSString *const BSCategoryID = @"category";
static NSString *const BSUserID = @"user";
#pragma mark - LazyLoad
-(BSRecommendCategoryViewModel *)reVM{
    if (_reVM == nil) {
        _reVM = [[BSRecommendCategoryViewModel alloc]init];
    }
    return _reVM;
}
-(BSCategoryUserListViewModel *)userVM{
    if (_userVM == nil) {
        _userVM = [[BSCategoryUserListViewModel alloc]init];
    }
    return _userVM;
}
-(BSRecommendViewModel *)recomVM{
    if (_recomVM == nil) {
        _recomVM = [[BSRecommendViewModel alloc]init];
    }
    return _recomVM;
}
-(UITableView *)leftTableView{
    if (_leftTableView == nil) {
        _leftTableView = [[UITableView alloc]init];
    }
    return _leftTableView;
}
-(UITableView *)rightTableView{
    if (_rightTableView == nil) {
        _rightTableView = [[UITableView alloc]init];
    }
    return _rightTableView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = BSGlobalColor;
    self.title = @"推荐关注";
    [self setupTableViewUI];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    [SVProgressHUD show];
    
    [self.leftTableView registerClass:[BSRecommendCategoryCell class] forCellReuseIdentifier:BSCategoryID];
    [self.rightTableView registerNib:[UINib nibWithNibName:NSStringFromClass([BSUserTableViewCell class]) bundle:nil] forCellReuseIdentifier:BSUserID];
    [self.recomVM getRecommendDataWithA:@"friend_recommend" C:@"user" completionHandler:^(NSError *error) {
        if (error) {
            BSLog(@"错误");
        }else{
            [self.rightTableView reloadData];
        }
    }];
    [self.reVM getListWithA:@"category" C:@"subscribe" completionHandler:^(NSError *error) {
        if (error) {
            [SVProgressHUD showErrorWithStatus:@"数据加载错误"];
        }else{
            [self.leftTableView reloadData];
            NSIndexPath *first = [NSIndexPath indexPathForRow:0 inSection:0];
            [self.leftTableView selectRowAtIndexPath:first animated:YES scrollPosition:UITableViewScrollPositionTop];
            [self.recomVM getRecommendDataWithA:@"friend_recommend" C:@"user" completionHandler:^(NSError *error) {
                if (error) {
                    BSLog(@"错误");
                }else{
                    [self.rightTableView reloadData];
                }
            }];
            [SVProgressHUD dismiss];
        }
    }];

    
}

-(void)setupTableViewUI{
    self.leftTableView.delegate = self;
    self.leftTableView.dataSource = self;
    self.leftTableView.backgroundColor = [UIColor clearColor];
    self.leftTableView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:self.leftTableView];
    NSDictionary *dictView = @{@"leftTableView":self.leftTableView,@"rightTableView":self.rightTableView};
    [self.view addConstraints:[NSLayoutConstraint
                              constraintsWithVisualFormat:@"V:|-[leftTableView]-|"
                              options:0
                              metrics:nil
                               views:dictView]];
    [self.view addConstraints:[NSLayoutConstraint
                              constraintsWithVisualFormat:@"H:|-(-5)-[leftTableView(80)]"
                              options:0
                              metrics:nil
                               views:dictView]];
    
    self.rightTableView.delegate = self;
    self.rightTableView.dataSource = self;
    self.rightTableView.backgroundColor = [UIColor clearColor];
    self.rightTableView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:self.rightTableView];
    [self.view addConstraints:[NSLayoutConstraint
                              constraintsWithVisualFormat:@"V:|-64-[rightTableView]-|"
                              options:0
                              metrics:nil
                               views:dictView]];
    [self.view addConstraints:[NSLayoutConstraint
                              constraintsWithVisualFormat:@"H:[leftTableView]-0-[rightTableView]-0-|"
                              options:0
                              metrics:nil
                               views:dictView]];
    
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView == self.leftTableView) {
        return [self.reVM rowNumber]+1;
    } else {
        return [self.userVM rowNumber];
    }
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == self.leftTableView) {
        return 44;
    }else{
        return 60;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == self.leftTableView) {
        BSRecommendCategoryCell *cell = [tableView dequeueReusableCellWithIdentifier:BSCategoryID forIndexPath:indexPath];
        self.leftTableView.separatorStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = BSGlobalColor;
        if (indexPath.row == 0) {
            cell.textLabel.text = @"推荐";
        }else{
           cell.textLabel.text = [self.reVM name:(indexPath.row-1)];
        }
        
        return cell;
    }else{
        BSUserTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:BSUserID forIndexPath:indexPath];
        self.rightTableView.separatorStyle = UITableViewCellSelectionStyleNone;
        if (self.leftTableView.indexPathForSelectedRow.row == 0) {
            cell.screenName.text = [self.recomVM screenName:indexPath.row];
            cell.fansCount.text = [NSString stringWithFormat:@"%@人关注",[self.recomVM fansCount:indexPath.row]];
            [cell.headerView sd_setImageWithURL:[NSURL URLWithString:[self.recomVM headerImageName:indexPath.row]] placeholderImage:[UIImage imageNamed:@"defaultUserIcon"]];
        }else{
            cell.screenName.text = [self.userVM screenName:indexPath.row];
            cell.fansCount.text = [NSString stringWithFormat:@"%ld人关注",[self.userVM fansCount:indexPath.row]];
            [cell.headerView sd_setImageWithURL:[NSURL URLWithString:[self.userVM headerImg:indexPath.row]] placeholderImage:[UIImage imageNamed:@"defaultUserIcon"]];
        }
        
        
        return cell;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == self.leftTableView) {
        if (indexPath.row == 0) {
            [self.recomVM getRecommendDataWithA:@"friend_recommend" C:@"user" completionHandler:^(NSError *error) {
                if (error) {
                    BSLog(@"错误");
                }else{
                    [self.rightTableView reloadData];
                }
            }];
        }else{
            [self.userVM getIDUserListWithA:@"list" c:@"subscribe" categoryID:[self.reVM ID:indexPath.row-1] completionHandler:^(NSError *error) {
                if (error) {
                    BSLog(@"1111");
                }else{
                    [self.rightTableView reloadData];
                }
            }];

        }
    }
    
}












@end
